import flash.display.Sprite;
import flash.display.Shape;

//Keyboard Events
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.MouseEvent;

import ShipModel;

class ShipView extends CircleView
{
  private var _model:ShipModel;
  private var _controller:ShipController;
  private var _stage:Dynamic;
  private var shape:Shape;
  //private override var model:ShipModel;
  public function new(model:ShipModel,controller:ShipController, stage:Dynamic)
  {
    super(model);
    this._model = model;
    this._controller = controller;
    this._stage = stage;
    _stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
    _stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
        shape = new Shape();
    shape.graphics.lineStyle(1);
    shape.graphics.beginFill(model.color);
    shape.graphics.drawCircle(0, 0, cast(model,CircleModel).radius);
    shape.graphics.endFill();
//     _stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
//     _stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
  }
  public function redraw()
  {
    removeShape();
    shape = new Shape();
    shape.graphics.lineStyle(1);
    shape.graphics.beginFill(model.color);
    shape.graphics.drawCircle(0, 0, cast(model,CircleModel).radius);
    shape.graphics.endFill();
    addShape();
  }
  private function keyDownHandler(event:KeyboardEvent)
  {
    _controller.processKeyDown(event);
  }
  private function keyUpHandler(event:KeyboardEvent)
  {
    _controller.processKeyUp(event);
  }
  private function mouseDownHandler(event:MouseEvent)
  {
    _controller.processMouseDown(event, _stage);
  }
  private function mouseUpHandler(event:MouseEvent)
  {
    _controller.processMouseUp(event);
  }
  override private function draw()
  {
    addChild(shape);
  }

  public function removeShape()
  {
    removeChild(shape);
  }

  public function addShape()
  {
    addChild(shape);
  }


}